<?php
namespace Blog\Controller;

use Common\Controller\HomebaseController;

class IndexController extends HomebaseController
{

    // 注册三部曲，第一步，接受到数据，判断数据是否符合标准，发送验证码（调用发送验证码的方法）
    public function index(){

/*      if(send_mimi_code('13865891681','1212')){
           echo "发送成功";
       }else{
           echo "发送失败";
       }*/
/*     echo "<form action='http://p.bucoo.cn/index.php?g=Blog&m=index&a=checkMobile' method='post'>";
     echo "<input type='text' name='mobile'>";
//     echo htmlspecialchars(sp_verifycode_img('length=4&font_size=14&width=120&height=34&charset=1234567890&use_noise=1&use_curve=0'));
//     echo  sp_verifycode_img('length=4&font_size=14&width=120&height=34&charset=1234567890&use_noise=1&use_curve=0');
     echo "<input type='submit'>";
     echo "</form>";*/

//     sp_check_verify_code();
/*        $data= array(
            'code' => 200,
            'message' => '成功',
            'data' => '123123213123213123123'
        );
        echo $this->ajaxReturn($data,'json');*/

    }

    public function mobileVerify ()
    {
        if (IS_POST) {
            $mobile =I('post.mobile/s');

        }
    }
    //发送手机验证码
    public function send ()
    {
       if(IS_GET){
        $mobile=I('get.mobile/s');
        $result = send_mobile_code('13865891681');
        if($result['error']===0){
            $result = array(
                'code' => true
            );
           return $result;
            die;
        }else{
            $result = array(
                'code' => false,
                'message' => $result['error_msg']
            );
            return $result;
        }
      }
    }


    /*验证码比较*/
    public function checkMobileCode ($phone,$code)
    {
        if (sp_check_mobile_verify_code($phone,$code)) {
            echo "验证成功" ;
        } else {
            echo "验证失败";
        }
    }

    public function test ()
    {
        echo 123;
    }


    //手机号验证,需要
    public function checkMobile()
    {
        $verify = D('Users','Logic');
        $data['mobile'] = I('request.mobile');
        if (!$verify->create($data)){
            // 返回json格式的错误信息
            $message = '手机号不能注册';
            $data = $verify->getError();
            $data= array(
                'code' => 400,
                'message' => '失败',
                'data' => $data
            );
            echo $this->ajaxReturn($data,'json');
        }else{
            // 发送短信验证码
            $c = A('Blog/index');
            $result = $c->send();
            $result = M('MobileCodeLog')->where('mobile=13865891681')->field('send_time')->find();
            if (time() - $result['send_time'] < 60 ) {
                $data = array(
                    'code' => 402,
                    'message' => '操作时间过短',
                    'data' => "htmlspecialchars(sp_verifycode_img('length=4&font_size=14&width=120&height=34&charset=1234567890&use_noise=1&use_curve=0'))"
                );
                echo $this->ajaxReturn(data,'json');
                exit;
            }

            if ($result['code']) {
                $data= array(
                    'code' => 200,
                    'message' => '成功',
                    'data' => '短信发送成功'
                );
                echo $this->ajaxReturn($data,'json');
            } else {
                $data= array(
                    'code' => 401,
                    'message' => '失败',
                    'data' => $result['message']
                );

               echo $this->ajaxReturn($data,'json');
            }

        }


     /*   $callback = $abcd->mobile('');
        if ($callback) {
            echo  '验证成功';
        }else{
            echo '验证失败';
        };*/
    }




    //生成验证码
    public function verify ()
    {
        echo htmlspecialchars(sp_verifycode_img('length=4&font_size=14&width=120&height=34&charset=1234567890&use_noise=1&use_curve=0'));
    }
    //验证码验证，返回json
    public function checkVerify()
    {
        if(!sp_check_verify_code()){
            echo '验证码不正确';
        }else{
            echo "验证码正确";
        }

    }



}